package com.group.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.group.pojo.TbRole;
import com.group.pojo.TbUser;
import com.group.service.RoleService;
import com.group.service.UserService;

/**
 * 用户管理
 * @author caoyanan
 * @time 2017年10月8日下午6:53:26
 */
@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;

	@RequestMapping("/login.action")
	@ResponseBody
	public Map<String, String> login(HttpServletRequest request, TbUser user) {
		Map<String, String> map = userService.login(request, user);
		return map;
	}
	
	@RequestMapping("/logout.action")
	public String logout(HttpServletRequest request) {
		request.getSession().removeAttribute("USER_SESSION");
		return "redirect:/login.jsp";
	}
	

	
	@RequestMapping("/page.action")
	public String user(Model model) {
		List<TbRole> allRoles = roleService.getAllRoles();
		model.addAttribute("allRoles",allRoles);
		return "admin/user/user";
	}
	
	@RequestMapping("/getLoginUser.action")
	@ResponseBody
	public TbUser getLoginUser(HttpServletRequest request) {
		TbUser user;
		try {
			user = userService.getLoginUser(request);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return user;
	}
	
	@RequestMapping("/query.action")
	@ResponseBody
	public List<TbUser> query(TbUser user) {
		List<TbUser> users = userService.query(user); 
		return users;
	}
	
	
	@RequestMapping("/getUserNameById.action")
	@ResponseBody
	public String getUserNameById(String id) {
		String userName = userService.getUserNameById(id); 
		return userName;
	}
	
	/**
	 * @param user
	 * @return (true:表示合法，数据库还没有此人)(false:表示数据不合法，数据库已经有此人了)
	 */
	@RequestMapping("/userCheckRepect.action")
	@ResponseBody
	public String userCheckRepect(TbUser user) {
		String isExist = userService.userCheckRepect(user); 
		return isExist;
	}
	
	@RequestMapping("/save.action")
	@ResponseBody
	public Map<String, String> save(HttpServletRequest request, TbUser user) {
		Map<String, String> map = userService.save(request, user); 
		return map;
	}
	
	@RequestMapping("/update.action")
	@ResponseBody
	public Map<String, String> update(TbUser user, HttpServletRequest request) {
		Map<String, String> map = userService.update(user, request); 
		return map;
	}
	
	@RequestMapping("/removeForbidden.action")
	public String removeForbidden() {
		return "admin/user/remove-forbidden";
	}
	
	
	@RequestMapping("/batchDelete.action")
	@ResponseBody
	public Map<String, String> batchDelete(@RequestParam(value="userIds[]") String[] userIds, HttpServletRequest request) {
		Map<String, String> map = userService.batchDelete(userIds, request); 
		return map;
	}
	
	@RequestMapping("/batchRemoveForbidden.action")
	@ResponseBody
	public Map<String, String> batchRemoveForbidden(@RequestParam(value="userIds[]") String[] userIds, HttpServletRequest request) {
		Map<String, String> map = userService.batchRemoveForbidden(userIds,request); 
		return map;
	}
}
